package dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pojo.ChangeRoom;
import service.BedServiceImpl;
import utils.JDBCUtils;
import utils.SystemEnum;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class ChangeRoomDaoImpl implements ChangeRoomDao{

    //调换信息
    private Log log = LogFactory.getLog(getClass());
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public ArrayList<ChangeRoom> findAll() {
        log.info(SystemEnum.BEGIN);
        ArrayList<ChangeRoom> list = new ArrayList<ChangeRoom>();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM change_record ";
            statement = con.prepareStatement(sql);
            rs = statement.executeQuery();
            while(rs.next()){
                ChangeRoom changeRoom = new ChangeRoom(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getInt(4),rs.getInt(5),rs.getInt(6),rs.getDate(7),rs.getInt(8));
                list.add(changeRoom);
            }
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement,rs);
            log.info(SystemEnum.END);
        }
        return list;
    }

    @Override
    public ChangeRoom findById(Integer changeId) {
        log.info(SystemEnum.BEGIN);
        ChangeRoom changeRoom = new ChangeRoom();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM change_record WHERE changeId = ?";
            statement = con.prepareStatement(sql);
            statement.setInt(1,changeId);
            rs = statement.executeQuery();
            while(rs.next()){
                changeRoom.setChangeId(rs.getInt(1));
                changeRoom.setStuId(rs.getInt(2));
                changeRoom.setOld_dormId(rs.getInt(3));
                changeRoom.setOld_bedId(rs.getInt(4));
                changeRoom.setNew_dormId(rs.getInt(5));
                changeRoom.setNew_bedId(rs.getInt(6));
                changeRoom.setChange_time(rs.getDate(7));
            }
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement,rs);
            log.info(SystemEnum.END);
        }
        return changeRoom;
    }

    @Override
    public int insert(Connection con,ChangeRoom changeRoom) throws SQLException {
        log.info(SystemEnum.BEGIN);
        PreparedStatement statement = null;
        ResultSet rs = null;
        int result = 0;
        try {
            String sql = "INSERT INTO change_record VALUES (?,?,?,?,?,?,?) ";
            statement = con.prepareStatement(sql);
            statement.setInt(1,changeRoom.getChangeId());
            statement.setInt(2,changeRoom.getStuId());
            statement.setInt(3,changeRoom.getOld_dormId());
            statement.setInt(4,changeRoom.getOld_bedId());
            statement.setInt(5,changeRoom.getNew_dormId());
            statement.setInt(6,changeRoom.getNew_bedId());
            statement.setDate(7, new java.sql.Date(changeRoom.getChange_time().getTime()));
            result = statement.executeUpdate();
            log.info("增加换房信息"+SystemEnum.SUCCESS);
        } catch (Exception e) {
            log.info("新增入住信息异常，执行回滚操作");
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public int update(ChangeRoom changeRoom) {
        log.info(SystemEnum.BEGIN);
        Connection con = null;
        PreparedStatement statement = null;
        PreparedStatement statement2 = null;
        int result = 0;
        try {
            con = JDBCUtils.getConnection();
            String sql = "UPDATE change_record SET stuId = ?,old_dormId = ?,old_bedId = ?,new_dormId = ?,new_bedId = ?,change_time = ? WHERE changeId = ?";
            statement = con.prepareStatement(sql);
            statement.setInt(1,changeRoom.getStuId());
            statement.setInt(2,changeRoom.getOld_dormId());
            statement.setInt(3,changeRoom.getOld_bedId());
            statement.setInt(4,changeRoom.getNew_dormId());
            statement.setInt(5,changeRoom.getNew_bedId());
            statement.setDate(6,new java.sql.Date(changeRoom.getChange_time().getTime()));
            statement.setInt(7,changeRoom.getChangeId());
            result = statement.executeUpdate();
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement);
            log.info(SystemEnum.END);
        }
        return result;
    }

    @Override
    public int delete(ChangeRoom changeRoom) {
        log.info(SystemEnum.BEGIN);
        Connection con = null;
        PreparedStatement statement = null;
        PreparedStatement statement2 = null;
        int result = 0;
        try {
            con = JDBCUtils.getConnection();
            //删除换房信息
            String sql = "DELETE FROM change_record (stuId,old_dormId,old_bedId,new_dormId,new_bedId,change_time,check_in_id) WHERE changeId = ? ";
            statement = con.prepareStatement(sql);
            statement.setInt(1,changeRoom.getChangeId());
            result = statement.executeUpdate();
            log.info("删除换房信息\t"+SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            log.info(SystemEnum.END);
            JDBCUtils.close(con,statement);
        }
        return result;
    }

}
